package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.whu.demo.dao.UserRegisterDao;
import edu.whu.demo.domain.UserRegister;
import edu.whu.demo.service.IUserRegisterService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class UserRegisterServiceImpl implements IUserRegisterService {
    @Autowired
    UserRegisterDao userRegisterDao;

    @Override
    public boolean addUserRegister(String name,String email,int sms){
        UserRegister userRegister1 =  userRegisterDao.selectById(name);
        if (userRegister1==null){
            UserRegister userRegister = new UserRegister();
            userRegister.setEmail(email);
            userRegister.setSms(sms);
            userRegister.setName(name);
            userRegister.setTime(fiveNextTime());
            userRegisterDao.insert(userRegister);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteUserRegister(String name) throws ParseException {
        UserRegister userRegister = userRegisterDao.selectById(name);
        if (userRegister==null){
            return true;
        }else {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date s1 = sdf1.parse(userRegister.getTime());
            Date s2 = new Date();
            long time = s1.getTime();
            long d2Time = s2.getTime();
            if (time>d2Time){
                return false;
            }else {
                userRegisterDao.deleteById(name);
                return true;
            }
        }
    }
    @Override
    public void updateUserRegister(UserRegister userRegister){
        userRegisterDao.updateById(userRegister);
    }
    @Override
    public void deleteUserRegisterOk(String name){
        userRegisterDao.deleteById(name);
    }
    @Override
    public UserRegister selectById(String name){
        return userRegisterDao.selectById(name);
    }

    private String  fiveNextTime(){
        Date date = new Date();
        Calendar cal = Calendar.getInstance();//创建时间工具类
        cal.setTime(date);//设置时间坐标
        cal.add(Calendar.MINUTE,5 );//给时间坐标加上minutes分钟
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(cal.getTime());
    }

    public List<UserRegister> getAllUserRegister(){
        return userRegisterDao.getAll();
    }
}
